<script setup lang="ts">
import { PureTableBar } from "@/components/RePureTableBar";
import { DictSelect } from "@/components/common/DictSelect";
import { ref } from "vue";
import { useGodown } from "./utils/hook";

import Refresh from "@iconify-icons/ep/refresh";

defineOptions({
  name: "Godown"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  childColumns,
  dataList,
  pagination,
  supplierList,
  depositoryList,
  useRenderIcon,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  openDialog,
  openStockDialog
} = useGodown();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="入库单号：" prop="stockCode">
        <el-input
          v-model="form.stockCode"
          placeholder="请输入"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="备货订单号：" prop="purchaseOrderCode">
        <el-input
          v-model="form.purchaseOrderCode"
          placeholder="请输入"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="入库方式：" prop="stockStatus">
        <DictSelect
          v-model="form.stockStatus"
          dict-type="GODOWN_TYPE"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="入库类型：" prop="depositoryType">
        <el-select
          v-model="form.depositoryType"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
          @change="form.depositoryId = null"
        >
          <el-option label="供应商仓库" :value="2" />
          <el-option label="佳叽仓库" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="入库仓库：" prop="depositoryId">
        <el-select
          v-if="form.depositoryType == 2"
          v-model="form.depositoryId"
          :disabled="!form.depositoryType"
          filterable
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.id"
            :label="item.supplierName"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-else
          v-model="form.depositoryId"
          :disabled="!form.depositoryType"
          filterable
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="item in depositoryList"
            :key="item.id"
            :label="item.depositoryName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务日期：" prop="times">
        <el-date-picker
          v-model="form.times"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
          class="!w-[250px]"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      class="mt-0"
      style="transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1)"
      title=""
      :columns="columns"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button type="primary" @click="openDialog()"> 直接入库 </el-button>
        <el-button type="primary" @click="openStockDialog()">
          备货入库
        </el-button>
        <el-button type="primary" plain> 导入库存 </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 128 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #expand="{ row }">
            <div class="ml-[40px]">
              <pure-table
                row-key="id"
                align-whole="center"
                :data="row.itemList"
                :columns="childColumns"
                :header-cell-style="{
                  background: 'var(--el-fill-color-light)',
                  color: 'var(--el-text-color-primary)'
                }"
              />
            </div>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
.el-button:focus-visible {
  outline: none;
}
//在全局设置
:deep(input[aria-hidden="true"]) {
  display: none !important;
}
</style>
